package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行关联查询
 */
public class JDBCDemo6 {
    public static void main(String[] args) {
        /*
            需求:查看王克晶所带班级的信息，列出:老师的名字，工资，所带班级的名字，楼层
            SELECT t.name,t.salary,c.name,c.floor
            FROM teacher t,class c
            WHERE t.id=c.teacher_id
            AND t.name='王克晶'

            内连接写法:
            SELECT t.name,t.salary,c.name,c.floor
            FROM teacher t
            JOIN class c ON t.id=c.teacher_id
            WHERE t.name='王克晶'
         */

        try (Connection connection = DBUtil.getConnection();){
            Statement statement = connection.createStatement();
            String sql = "SELECT t.name,t.salary,c.name,c.floor " +
                         "FROM teacher t " +
                         "JOIN class c ON t.id=c.teacher_id " +
                         "WHERE t.name='王克晶' ";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                String tName = rs.getString("t.name");
                int tSalary = rs.getInt("t.salary");
                String cName = rs.getString("c.name");
                int cFloor = rs.getInt("c.floor");
                //String提供了将多个字符串链接为一个字符串，并在每个字符串中间添加【参数一】作为分隔符
                String line = String.join(",",tName,tSalary+"",cName,cFloor+"");
                System.out.println(line);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}









